<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{category.top.name}}</XtxBreadItem>
    <transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="`category.sub.id`" v-if="category.sub">{{category.sub.name}}</XtxBreadItem>
    </transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    // 通过计算属性从vuex获取一级类目和二级类目的ID与名称
    // 对象：{top:{id,name},sub:{id,name}}
    const route = useRoute()
    const store = useStore()
    const category = computed(() => {
      const cate = {}
      // 完成获取数据的逻辑
      store.state.category.list.forEach(top => {
        top.children && top.children.forEach(sub => {
          if (sub.id === route.params.id) {
            // 设置二级类目
            cate.sub = { id: sub.id, name: sub.name }
            // 设置一级类目
            cate.top = { id: top.id, name: top.name }
          }
        })
      })
      return cate
    })
    return { category }
  }
}
</script>

<style lang="less" scoped>

</style>
